﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace OODCRM.Dialogs
{
    /// <summary>
    /// Interaction logic for UserLogin.xaml
    /// </summary>
    public partial class UserLogin : Window
    {

        User MyUser { get; set; }
        string ConnectionString { get; set; }

        public UserLogin()
        {
            InitializeComponent();
            tbxPassword.PasswordChar = '>';
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string user = tbxUsername.Text;
            string pass = tbxPassword.Password;

            bool isOK = true;
            for (int i = 0; i < user.Length && isOK; i++)
            {
                if ((user[i] >= 'A' && user[i] <= 'Z')
                    || (user[i] >= 'a' && user[i] <= 'z')
                    || (user[i] >= '0' && user[i] <= '9')
                    || (user[i] == '_')
                    ) ;
                else isOK = false;
            }

            if (!isOK)
            {
                MessageBox.Show("Tên người dùng không hợp lệ!");
                return;
            }

            isOK = true;
            for (int i = 0; i < pass.Length && isOK; i++)
            {
                if ((pass[i] >= 'A' && pass[i] <= 'Z')
                    || (pass[i] >= 'a' && pass[i] <= 'z')
                    || (pass[i] >= '0' && pass[i] <= '9')
                    || (pass[i] == '_')
                    ) ;
                else isOK = false;
            }

            if (!isOK)
            {
                MessageBox.Show("Mật khẩu không hợp lệ!");
                return;
            }

            CRMDataDataContext dataContext = new CRMDataDataContext();
            SqlConnection connection = new SqlConnection();
            try
            {
                connection = new SqlConnection(dbSelector.DBConnectionString);
                connection.Open();

                ConnectionString = dbSelector.DBConnectionString;
                Properties.Settings.Default.User_ConnectionString = dbSelector.DBConnectionString;
                dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);

                var u = dataContext.Users.FirstOrDefault(x => x.Username == user && x.Password == pass);
                if (u != null)
                {
                    MyUser = u;
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show("Thông tin đăng nhập người dùng không hợp lệ!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối CSDL! Kiểm tra thông tin kết nối!");
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }
    }
}
